package com.cx.common.utils;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author:cxy
 * @Date:2019/10/24 15:11
 */
public class CookiesUtil {

	public static void addCookies(String name, String value) {
		HttpServletResponse response = getResponse();
		Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");
        cookie.setMaxAge(Integer.MAX_VALUE);
        response.addCookie(cookie);
	}

    /**
     * 存取当前cookie的浏览记录
     * @param response
     * @param name
     * @param value
     */
    public static void searchCookies(HttpServletResponse response, String name, String value) {
        Cookie cookie = getCookie(name);
        if(cookie == null){
            cookie = new Cookie(name,value);
        }else{
            List<String> l = Arrays.asList(cookie.getValue().split(Splitable.ATTRIBUTE_SPLITE_1));
            LinkedList<String> list = new LinkedList<>(l);
            if(list.contains(value)){
                list.remove(value);
                list.addFirst(value);
            }else{
                if(list.size()==15){
                    list.removeLast();
                }
                list.addFirst(value);
            }
            String s = CollectionsUtils.joinArrayWithSeparator(list, Splitable.ATTRIBUTE_SPLITE_1);
            cookie.setValue(s);
        }
        cookie.setMaxAge(Integer.MAX_VALUE);
        response.addCookie(cookie);
    }

    /**
     * 获取searchCookie
     * @param request
     * @param name
     * @return
     */
    public static List<String> getSearchKeyList(String name){
        Cookie cookie = getCookie(name);
        if (cookie == null) {
            return New.arrayList();
        }
        return Arrays.asList(cookie.getValue().split(Splitable.ATTRIBUTE_SPLITE_1));
    }

    /**
     * 移除cookie
     * @param request
     * @param response
     * @param name 这个是名称，不是值
     */
    public static void removeCookie(String name) {
        if (null == name) {
            return;
        }
        Cookie cookie = getCookie(name);
        if(null != cookie){
            cookie.setValue("");
            cookie.setMaxAge(0);
            HttpServletResponse response = getResponse();
            response.addCookie(cookie);
        }
    }

    /**
     * 获取cookie
     * @param request
     * @param name
     * @return
     */
    public static Cookie getCookie(String name) {
    	HttpServletRequest request = getRequest();
        Cookie[] cookies = request.getCookies();
        if (ArrayUtils.isEmpty(cookies)) {
            return null;
        }
        Cookie cookie = null;
        for (Cookie c : cookies) {
            if (StringUtils.equals(c.getName(),name)) {
                cookie = c;
                break;
            }
        }
        return cookie;
    }


    public static String getCookieValue( HttpServletRequest request,String name) {

        Cookie[] cookies = request.getCookies();
        if (ArrayUtils.isEmpty(cookies)) {
            return null;
        }
        Cookie cookie = null;
        for (Cookie c : cookies) {
            if (StringUtils.equals(c.getName(),name)) {
                cookie = c;
                break;
            }
        }
        if(cookie == null) {
            return StringUtils.EMPTY;
        }
        return cookie.getValue();
    }

    public static String getCookieValue(String name) {

    	HttpServletRequest request = getRequest();
        Cookie[] cookies = request.getCookies();
        if (ArrayUtils.isEmpty(cookies)) {
            return null;
        }
        Cookie cookie = null;
        for (Cookie c : cookies) {
            if (StringUtils.equals(c.getName(),name)) {
                cookie = c;
                break;
            }
        }
        if(cookie == null) {
        	return StringUtils.EMPTY;
        }
        return cookie.getValue();
    }

    private static HttpServletRequest getRequest() {
    	RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        return request;
    }

    private static HttpServletResponse getResponse() {
    	RequestAttributes ra = RequestContextHolder.getRequestAttributes();
    	ServletRequestAttributes sra = (ServletRequestAttributes) ra;
    	HttpServletResponse response = sra.getResponse();
    	return response;
    }
}

